package homework.gui;

import com.lovo.netCRM.component.LovoTable;
import homework.util.DataUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class OptionPanel extends JPanel {

    private JButton jButton1,jButton2,jButton3,jButton4,jButton5;

    private StudentFrame studentFrame;

    public  OptionPanel(StudentFrame studentFrame){

        this.studentFrame = studentFrame;

        jButton1=new JButton("添加学生");
        jButton2=new JButton("查询学生详细信息");
        jButton3=new JButton("修改学生信息");
        jButton4=new JButton("删除");
        jButton5 = new JButton("保存数据");

        //添加按钮
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                 //创建添加面板
                 new AddStudentDialog(studentFrame);
            }
        });

        //查询学生信息
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获得显示面板中的数据表格
                LovoTable lovoTable = studentFrame.getShowTablePanel().getDatas();
                //得到表格中选中的id号
                int id  = lovoTable.getKey();
                if(id!=-1){
                     //显示查询的对话框
                    new showStudentInfoDialog(studentFrame,id);

                }else{
                    JOptionPane.showMessageDialog(null,"请先选中一行");
                }


            }
        });

        //修改按钮
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获得显示面板中的数据表格
                LovoTable lovoTable = studentFrame.getShowTablePanel().getDatas();
                //得到表格中选中的id号
                int id  = lovoTable.getKey();

                if(id!=-1){
                    new UpdateStudentDialog(studentFrame,id);
                }else{
                    JOptionPane.showMessageDialog(null,"请先选中一行");

                }


            }
        });



        //删除按钮
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获得显示面板中的数据表格
                LovoTable lovoTable = studentFrame.getShowTablePanel().getDatas();
                //得到表格中选中的id号
                int id  = lovoTable.getKey();
                //判断是否选中一行
                if(id!=-1){

              //显示一个确认对话框
                 int type =    JOptionPane.showConfirmDialog(null,"你确定要删除吗?");

                 if(type==0){
                     //删除数据
                    DataUtil.deleteStudent(id);
                    //在删除数据表格中的行
                    lovoTable.removeRow();
                    //显示提示语句
                    JOptionPane.showMessageDialog(null,"删除成功");
                 }

                }else{
                    JOptionPane.showMessageDialog(null,"请先选中一行");
                }


            }

        });

        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File f = new File("E:\\javaooGit\\javaooDay22\\photo");
                DataUtil.serializableStudentAndClass();
                JOptionPane.showMessageDialog(null,"保存成功");
            }
        });

        this.add(jButton1);
        this.add(jButton2);
        this.add(jButton3);
        this.add(jButton4);
        this.add(jButton5);




    }
}
